import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;

public class Missile {
	private int x, y;
	private boolean live = true;
	private boolean good;
	public static final int WIDTH = 10;
	public static final int HEIGHT = 10;

	public static final int XSPEED = 10;
	public static final int YSPEED = 10;

	private TankClient tc;
	private Direction dir;

	public Missile(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public Missile(int x, int y, TankClient tc) {
		this(x, y);
		this.tc = tc;
	}

	public Missile(int x, int y, Direction dir, TankClient tc) {
		this(x, y, tc);
		this.dir = dir;
	}

	public Missile(int x, int y, Direction dir, boolean good, TankClient tc) {
		this(x, y, dir, tc);
		this.good = good;
	}

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public void draw(Graphics g) {
		// TODO Auto-generated method stub
		if (!live) {
			tc.missiles.remove(this);
			return;
		}
		Color c = g.getColor();
		g.setColor(Color.BLACK);
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		move();
	}

	private void move() {
		// TODO Auto-generated method stub
		switch (dir) {
		case R:
			x += XSPEED;
			break;
		case RU:
			x += XSPEED;
			y -= YSPEED;
			break;
		case U:
			y -= YSPEED;
			break;
		case LU:
			x -= XSPEED;
			y -= YSPEED;
			break;
		case L:
			x -= XSPEED;
			break;
		case LD:
			x -= XSPEED;
			y += YSPEED;
			break;
		case D:
			y += YSPEED;
			break;
		case RD:
			x += XSPEED;
			y += YSPEED;
			break;
		case STOP:
			break;

		}

		if (x <= 0 || x >= TankClient.GAME_WIDTH || y <= 0
				|| y >= TankClient.GAME_HEIGHT)
			this.live = false;
	}

	public Rectangle getRect() {
		return new Rectangle(x, y, Missile.WIDTH, Missile.HEIGHT);
	}

	public void hitTank(Tank tank) {
		if (this.isLive() && this.getRect().intersects(tank.getRect())
				&& tank.isLive() && this.good != tank.isGood()) {
			if (tank.isGood()) {
				tank.setLife(tank.getLife() - 20);
				if (tank.getLife() <= 0)
					tank.setLive(false);
			} else {
				tank.setLive(false);
			}
			this.live = false;
			Explode e = new Explode(x, y,tc);
			tc.explodes.add(e);
		}
	}

	public void hitTanks(List<Tank> tanks) {
		// TODO Auto-generated method stub
		for (int i = 0; i < tanks.size(); i++) {
			Tank tank = tanks.get(i);
			this.hitTank(tank);
		}
	}

	public void hitWall(Wall wall) {
		if (this.isLive() && this.getRect().intersects(wall.getRect()))
			this.setLive(false);
	}
}
